//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "TGenValForm.h"
#include "TMainForm.h"
#include "TFormatEditForm.h"
#include "Math.h"
#include "Math.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CSPIN"
#pragma resource "*.dfm"
TGenValForm *GenValForm;
//---------------------------------------------------------------------------
__fastcall TGenValForm::TGenValForm(TComponent* Owner)
        : TForm(Owner),
   Begin(-10),
   End(10)     
{
   XFormat = new TNumber;
   YFormat = new TNumber;
}
//---------------------------------------------------------------------------
void __fastcall TGenValForm::FormCreate(TObject *Sender)
{
   SG->Cells[1][0] = "X";
   SG->Cells[2][0] = "Y";
   PointsCountCSpin->Value = 10;
}

//---------------------------------------------------------------------------
void __fastcall TGenValForm::Define()
{
   if (PointsCountCSpin->Value < 0) PointsCountCSpin->Value = 1;
   if (PointsCountCSpin->Value > 1000) PointsCountCSpin->Value = 1000;
   Count = PointsCountCSpin->Value;
   SG->RowCount = Count + 1;
}

//---------------------------------------------------------------------------
void TGenValForm::AssignPointer(TGRA * _GRA, TGraphForm *_GraphForm)
{
  GRA = _GRA;
  GraphForm = _GraphForm;
}
//---------------------------------------------------------------------------
void __fastcall TGenValForm::FormShow(TObject *Sender)
{
  Define();
  if (GRA->IsBegin) {
    Begin = GRA->Begin;
    BeginEdit->Text = FloatToStr(Begin);
  }
  else {
    Begin = GraphForm->Area->CoordHor(GraphForm->Area->Rc.Left);
    BeginEdit->Text = FloatToStr(Begin);
  };
  if (GRA->IsEnd) {
    End = GRA->End;
    BeginEdit->Text = FloatToStr(End);
  }
  else {
    End = GraphForm->Area->CoordHor(GraphForm->Area->Rc.Right);
    EndEdit->Text = FloatToStr(End);
  };
  Clear();
}

//---------------------------------------------------------------------------
void __fastcall TGenValForm::GenBtnClick(TObject *Sender)
{
  Define();
  Clear();
  Begin = BeginEdit->Text.ToDouble();
  End = EndEdit->Text.ToDouble();
  Tab = (End - Begin)/Count;
  double Val = Begin;
  for(int i = 1; i <= Count; i++) {
    SG->Cells[0][i] = IntToStr(i);
    SG->Cells[1][i] = XFormat->Convert(Val);
    SG->Cells[2][i] = YFormat->Convert(GRA->F2(Val));
    if (!GRA->Calc->Exist)
      SG->Cells[2][i] = "-";
    Val += Tab;
  }
 /* switch (MethodRG->ItemIndex) {
    case 0: {
      Tab = (End - Begin)/Count;
      double Val;
      Val = Begin;
      for(int i = 1; i <= Count; i++) {
        SG->Cells[0][i] = IntToStr(i);
        SG->Cells[1][i] = XFormat->Convert(Val);
        SG->Cells[2][i] = YFormat->Convert(GRA->F2(Val));
        if (!GRA->Calc->Exist)
          SG->Cells[2][i] = "-";
        Val += Tab;
      }
    } break;
    case 1: {
      Tab = (End - Begin)/10000;
      double Val;
      double ats;
      Val = Begin;
      for(int i = 1; i <= 10000; i++) {
        ats = GRA->F2(Val);
        if ((StrToFloat(FloatToStr(Val)) == Val) && (StrToFloat(FloatToStr(ats)) == ats)) {
          SG->Cells[0][i] = IntToStr(i);
          SG->Cells[1][i] = XFormat->Convert(Val);
          SG->Cells[2][i] = YFormat->Convert(ats);
          if (!GRA->Calc->Exist)
            SG->Cells[2][i] = "-";
          Val += Tab;
        }
      }
    } break;
    case 2: {
      Tab = (End - Begin)/10000;
      double Val;
      double ats;
      Val = Begin;
      for(int i = 1; i <= 10000; i++) {
        ats = GRA->F2(Val);
        if (IsZero( Fraction(ats) )) {
          SG->Cells[0][i] = IntToStr(i);
          SG->Cells[1][i] = XFormat->Convert(Val);
          SG->Cells[2][i] = YFormat->Convert(ats);
          if (!GRA->Calc->Exist)
            SG->Cells[2][i] = "-";
          Val += Tab;
        }
      }
    } break;
  }  */

}
//---------------------------------------------------------------------------
TGenValForm::Clear()
{
  for(int i = 1; i <= Count; i++) {
    SG->Cells[0][i] = "";
    SG->Cells[1][i] = "";
    SG->Cells[2][i] = "";
  }
}
//---------------------------------------------------------------------------
void __fastcall TGenValForm::F1BtnClick(TObject *Sender)
{
  AnsiString Str = "Labas";
  WideString S = Str;
//  MainForm->F1->LaunchDesigner();
}
//---------------------------------------------------------------------------

void __fastcall TGenValForm::Button1Click(TObject *Sender)
{
  FormatEditForm->SetCurFormat(XFormat);
  FormatEditForm->ShowModal();
  GenBtnClick(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TGenValForm::Button2Click(TObject *Sender)
{
  FormatEditForm->SetCurFormat(YFormat);
  FormatEditForm->ShowModal();
  GenBtnClick(Sender);
}


void __fastcall TGenValForm::CloseBtnClick(TObject *Sender)
{
  Close();        
}
//---------------------------------------------------------------------------


